package com.net.xpay.common.manager.impl.stat;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.stat.UserTransMonthStatManager;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.stat.UserTransMonthStatMapper;
import com.net.xpay.common.domain.stat.UserTransMonthStat;
import com.net.xpay.common.model.UserUnionTransMonthStatResultModel;
import com.net.xpay.common.model.search.UserUnionTransMonthStatSearchModel;
import com.net.xpay.core.model.UserTransMonthStatSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class UserTransMonthStatManagerImpl extends AbstractManager<UserTransMonthStat> implements UserTransMonthStatManager {

    @Autowired
    private UserTransMonthStatMapper mapper;


    @Override
    protected BasicMapper<Long, UserTransMonthStat> getMapper() {
        return mapper;
    }

    @Override
    public UserTransMonthStat findByMonthOfYear(Integer year, Integer month, Long userId) {
        Preconditions.checkArgument(year != null && month != null && userId != null);
        UserTransMonthStat condition = new UserTransMonthStat();
        condition.setYear(year);
        condition.setMonth(month);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<UserTransMonthStat> search(UserTransMonthStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }


    @Override
    public long searchCount(UserTransMonthStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public List<UserTransMonthStat> list(UserTransMonthStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public List<UserUnionTransMonthStatResultModel> searchUnion(UserUnionTransMonthStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        page.addOrder("create_time", "desc");
        return mapper.searchUnion(searchModel, page) == null ? new ArrayList<>() : mapper.searchUnion(searchModel, page) ;
    }

    @Override
    public long searchUnionCount(UserUnionTransMonthStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchUnionCount(searchModel);
    }
}
